<?php
//namespace dbmgr;
include "dbConn_config.php";

// check the database connection situation and print the error information
function dbConnCheck( $dbConn ){
	if ($dbConn->connect_error) {
		die("Connect Error (" . $dbConn->connect_errno . ") ". $dbConn->connect_error);
		$error_msg = $dbConn->connect_errno . $dbConn->connect_error;
		return $error_msg;
	}else if ($dbConn){
		return true;
	}else{
		echo "<p>unkown fail!</p>";
		return false;
	}
}

	// program entrance
	function dbConn(){
		$dbConnObject = new mysqli(HOST, USERNAME, PASSWORD, DBNAME, PORT);
		if(dbConnCheck($dbConnObject)){
			return $dbConnObject;
		}else{
			return dbConnCheck($dbConnObject);
		}
	}

class dbConn{
	function __construct($host, $usr, $pwd, $db, $port){
		$dbConnObject = new mysqli($host, $usr, $pwd, $db, $port);
		if ($dbConn->connect_error) {
			die("Connect Error (" . $dbConn->connect_errno . ") ". $dbConn->connect_error);
			$error_msg = $dbConn->connect_errno . $dbConn->connect_error;
			return $error_msg;
		} elseif ($dbConnObject) {
			return $dbConnObject;
		} else {
			return "Unkown reason to cause database connection error!";
		}
	}
}

class dbConnDefault extends dbConn{
	function __construct(){
		parent::__construct(HOST, USERNAME, PASSWORD, DBNAME, PORT);
	}
}
?>
